#!/usr/bin/env python3
import logging
import os
from concurrent.futures import ThreadPoolExecutor, as_completed

# module: cos-python-sdk-v5
from qcloud_cos import CosConfig, CosS3Client


class CosUploader:
    def __init__(self):
        self.region = 'ap-shanghai'
        self.secretId = 'SecretId'
        self.secretKey = 'SecretKey'
        self.bucketName = 'bucketName'

        # 设置日志输出
        logging.basicConfig(level=logging.INFO)

        # 创建配置对象
        self.config = CosConfig(Region=self.region, SecretId=self.secretId, SecretKey=self.secretKey)

        # 创建cos客户端
        self.client = CosS3Client(self.config)


    # 文件上传函数
    def upload_file(self, local_file_path, cos_object_name):
        try:
            response = self.client.upload_file(
                Bucket=self.bucketName,  # 存储桶名称
                LocalFilePath=local_file_path,
                Key=cos_object_name,
            )
            print(f"File {os.path.basename(local_file_path)} uploaded successfully to {cos_object_name}.")

            # 检查上传是否成功，并删除本地文件
            if response.get('ETag'):  # 假设上传成功会返回ETag
                os.remove(local_file_path)
                print(f"Deleted local file: {local_file_path}")
            else:
                print(f"Upload of {os.path.basename(local_file_path)} did not return an ETag, skipping deletion.")
        except Exception as e:
            print(f"Failed to upload {os.path.basename(local_file_path)}. Error: {e}")


# 批量上传并删除函数
    def upload_and_delete_files_multithreaded(self, local_dir, cos_dir, max_workers=5):
        files_to_upload = []
        for root, dirs, files in os.walk(local_dir):
            for file_name in files:
                local_file_path = os.path.join(root, file_name)
                relative_path = os.path.relpath(local_file_path, local_dir)
                cos_object_name = os.path.join(cos_dir, relative_path).replace("\\", "/")
                files_to_upload.append((local_file_path, cos_object_name))

        with ThreadPoolExecutor(max_workers=max_workers) as executor:
            futures = [executor.submit(self.upload_file, local_path, cos_path) for local_path, cos_path in files_to_upload]
            for future in as_completed(futures):
                future.result()  # 获取结果并捕获异常


# 调用函数上传文件并删除本地文件
if __name__ == "__main__":
    app = CosUploader()
    local_directory = '/data/dir'  # 替换为本地文件夹路径
    cos_directory = 'dir/'  # 替换为COS目录路径
    app.upload_and_delete_files_multithreaded(local_directory, cos_directory, max_workers=10)  # 设置最大线程数